package com.foreveross.fetl.core.domain.task.repository.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;

import com.foreveross.fetl.common.jpa.EntityOracleHibernateRepository;
import com.foreveross.fetl.core.domain.task.ExcelInfoEntity;
import com.foreveross.fetl.core.domain.task.repository.ExcelInfoRepository;
@Repository("excelInfoRepository")
public class ExcelInfoRepositoryImpl extends EntityOracleHibernateRepository
		implements ExcelInfoRepository {

	@Override
	public void saveOrUpdate(ExcelInfoEntity excelInfo) {
		 super.save(excelInfo);
	}

	@Override
	public void delete(ExcelInfoEntity excelInfo) {
		super.delete(ExcelInfoEntity.class, excelInfo.getId());

	}

	@Override
	public ExcelInfoEntity findById(Long id) {
		return super.get(ExcelInfoEntity.class, id);
	}

	@Override
	public List<ExcelInfoEntity> findByTime(String date,String fileType,Integer type) {
		List<Object> list = new ArrayList<Object>();
		StringBuffer sbf = new StringBuffer(
				"from ExcelInfoEntity e where 1=1 ");
		if(null!=date && !"".equals(date)){
			sbf.append(" and e.dateType < ? ");
			list.add(date);
		}
		if(null!=fileType && !"".equals(fileType)){
			sbf.append(" and e.fileType = ? ");
			list.add(fileType);
		}
		if(null!=type && type>0){
			sbf.append(" and e.type = ? ");
			list.add(type);
		}
		List<ExcelInfoEntity> listEntity =super.queryResult(sbf.toString(), list.toArray());
		return listEntity;
	}

	@Override
	public List<ExcelInfoEntity> findBycode(String code, String date,String fileType,Integer type) {
		List<Object> list = new ArrayList<Object>();
		StringBuffer sbf = new StringBuffer(
				"from ExcelInfoEntity e where 1=1 ");
		if(null!=code && !"".equals(code)){
			sbf.append(" and e.code =?  ");
			list.add(code);
		}
		if(null!=date && !"".equals(date)){
			String start =date +"00";
			String end =date +"23";
			sbf.append(" and e.dateType >= ? and e.dateType <= ? ");
			list.add(start);
			list.add(end);
		}
		if(null!=fileType && !"".equals(fileType)){
			sbf.append(" and e.fileType = ? ");
			list.add(fileType);
		}
		if(null!=type && type>0){
			sbf.append(" and e.type = ? ");
			list.add(type);
		}
		List<ExcelInfoEntity> listEntity =super.queryResult(sbf.toString(), list.toArray());
		return listEntity;
	}

	@Override
	public List<ExcelInfoEntity> findBycodeAndTime(String code, String date,String fileType,Integer type) {
		List<Object> list = new ArrayList<Object>();
		StringBuffer sbf = new StringBuffer(
				"from ExcelInfoEntity e where 1=1  ");
		if(null!=code && !"".equals(code)){
			sbf.append(" and e.code =?  ");
			list.add(code);
		}
		if(null!=date && !"".equals(date)){
			sbf.append(" and e.dateType = ? ");
			list.add(date);
		}
		if(null!=fileType && !"".equals(fileType)){
			sbf.append(" and e.fileType = ? ");
			list.add(fileType);
		}
		if(null!=type && type>0){
			sbf.append(" and e.type = ? ");
			list.add(type);
		}
		List<ExcelInfoEntity> listEntity =super.queryResult(sbf.toString(), list.toArray());
		return listEntity;
	}

	@Override
	public List<ExcelInfoEntity> findAll() {
		return super.findAll(ExcelInfoEntity.class);
	}

	@Override
	public ExcelInfoEntity findByName(String name,String fileType,Integer type) {
		List<Object> list = new ArrayList<Object>();
		StringBuffer sbf = new StringBuffer(
				"from ExcelInfoEntity e where 1=1 ");
		if(null!=name && !"".equals(name)){
			sbf.append(" and e.name =?  ");
			list.add(name);
		}
		if(null!=fileType && !"".equals(fileType)){
			sbf.append(" and e.fileType = ? ");
			list.add(fileType);
		}
		if(null!=type && type>0){
			sbf.append(" and e.type = ? ");
			list.add(type);
		}
		List<ExcelInfoEntity> listEntity =super.queryResult(sbf.toString(), list.toArray());
		if(null!=listEntity && listEntity.size()>0){
			return listEntity.get(0);
		}
		return null;
	}

}
